package org.example.ExperimentFifth;

import java.util.Stack;

public class TwoStack {
//    创建两个Stack模拟栈
    private Stack<String> stack1;
    private Stack<String> stack2;

    public TwoStack() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }
//出队列
    public void push(String task) {
        stack1.push(task);
    }
//进入队列
    public String poll() {
//        判断是否为空
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
//top输出栈顶元素
    public String top() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();
    }
//判断是否为孔
    public boolean isEmpty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }

    public static void main(String[] args) {
        TwoStack queue = new TwoStack();

        // 存储任务名称
        System.out.println("队列中的元素依次为：");
        queue.push("Task1");
        queue.push("Task2");
        queue.push("Task3");
        queue.push("Task4");

        // 打印队列中的所有任务名称
        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
    }
}
